package cn.dxj1016.lesson03;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

//还是要等待客户端的连接
public class UdpServerDemo01 {
    public static void main(String[] args) throws IOException {
//        开放端口
        DatagramSocket datagramSocket = new DatagramSocket(9090);
//        接收数据包
        byte[] bytes = new byte[1024];
        DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length);//接收
        datagramSocket.receive(datagramPacket);//阻塞接收
        System.out.println(datagramPacket.getAddress().getHostAddress());
        System.out.println(new String(datagramPacket.getData(), 0, datagramPacket.getLength()));
//        关闭连接
        datagramSocket.close();
    }
}
